package com.atguigu.service.impl;

import com.atguigu.dao.UserDao;
import com.atguigu.dao.impl.UserDaoImpl;
import com.atguigu.pojo.SysUser;
import com.atguigu.service.UserService;
import com.atguigu.utils.MD5Utils;

import java.sql.SQLException;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     * @throws SQLException
     */
    @Override
    public SysUser login(String username, String password) throws SQLException {
        //1.对password进行加密
        password = MD5Utils.encrypt(password);
        //2.传递到Dao层
        SysUser sysUser = userDao.login(username, password);
        return sysUser;
    }

    /**
     * 修改用户密码
     * @param uid
     * @param password  原始密码
     * @param newpassword  新密码
     * @return
     * @throws SQLException
     */
    @Override
    public boolean updateUserPassword(Integer uid, String password, String newpassword) throws SQLException {
        SysUser userById = userDao.findUserById(uid);
        String password1 = userById.getPassword();
        password = MD5Utils.encrypt(password);
        if(password.equals(password1)){
            userDao.updateUserPassword(uid,MD5Utils.encrypt(newpassword));
            return true;
        }
        return false;
    }
}
